﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace SimpleSM
{
    class Session : DB
    {
        public DataTable GetA(int group_id = 0)
        {
            if (group_id == 0)
            {
                return this.Select("SELECT * FROM sessions WHERE group_id IS NULL ORDER BY name ASC;");
            }
            else
            {
                return this.Select("SELECT * FROM sessions WHERE group_id = @group_id ORDER BY name ASC;", new string[1, 2] { { "@group_id", group_id.ToString() } });
            }
        }

        public DataTable Get(int id)
        {
            return this.Select("SELECT * FROM sessions WHERE id = @id;", new string[1, 2] { { "@id", id.ToString() } });
        }

        public bool Add(string name, string host, int group_id)
        {
            if (group_id == 0)
            {
                return this.NonSelect("INSERT INTO sessions (name, host, group_id) VALUES (@name, @host, NULL);", new string[2, 2] { { "@name", name }, { "@host", host } });
            }
            else
            {
                return this.NonSelect("INSERT INTO sessions (name, host, group_id) VALUES (@name, @host, @group_id);", new string[3, 2] { { "@name", name }, { "@host", host }, { "@group_id", group_id.ToString() } });
            }
        }

        public bool Update(int id, string name)
        {
             return this.NonSelect("UPDATE sessions SET name = @name WHERE id = @id;", new string[2, 2] { { "@name", name }, { "@id", id.ToString() } });
        }

        public bool UpdateHost(int id, string host)
        {
            return this.NonSelect("UPDATE sessions SET host = @host WHERE id = @id;", new string[2, 2] { { "@host", host }, { "@id", id.ToString() } });
        }

        public bool UpdateInfo(int id, string info)
        {
            return this.NonSelect("UPDATE sessions SET info = @info WHERE id = @id;", new string[2, 2] { { "@info", info }, { "@id", id.ToString() } });
        }

        public bool Update(int id, int group_id)
        {
            if (group_id == 0)
            {
                return this.NonSelect("UPDATE sessions SET group_id = NULL WHERE id = @id;", new string[1, 2] { { "@id", id.ToString() } });
            }
            else
            {
                return this.NonSelect("UPDATE sessions SET group_id = @group_id WHERE id = @id;", new string[2, 2] { { "@id", id.ToString() }, { "@group_id", group_id.ToString() } });
            }
        }

        public bool Delete(int id)
        {
            return this.NonSelect("DELETE FROM sessions WHERE id = @id;", new string[1, 2] { { "@id", id.ToString() } });
        }
    }
}
